﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Dack_Web2.DTO;
using System.Data.OleDb;

namespace Dack_Web2.DAO
{
    public class CongTyDAO
    {
        public void Insert(CongTyDTO tmp)
        {
            OleDbConnection cn = Sql_DataProvider.SQLConnect();
            string str = "insert into CongTy(TenDangNhap, TenCongTy, DiaChi, SoDienThoai) values (@TenDangNhap, @TenCongTy, @DiaChi, @SoDienThoai)";
            OleDbCommand cmd = new OleDbCommand(str, cn);

            OleDbParameter para1 = new OleDbParameter("@TenDangNhap", tmp.TenDangNhap);
            OleDbParameter para2 = new OleDbParameter("@TenCongTy", tmp.TenCongTy);
            OleDbParameter para3 = new OleDbParameter("@DiaChi", tmp.DiaChi);
            OleDbParameter para4 = new OleDbParameter("@SoDienThoai", tmp.SoDienThoai);

            cmd.Parameters.Add(para1);
            cmd.Parameters.Add(para2);
            cmd.Parameters.Add(para3);
            cmd.Parameters.Add(para4);

            cmd.ExecuteNonQuery();

            cn.Close();
        }

        public void Update(CongTyDTO tmp)
        {
            OleDbConnection cn = Sql_DataProvider.SQLConnect();
            string str = "update CongTy set TenCongTy=@TenCongTy, DiaChi=@DiaChi, SoDienThoai=@SoDienThoai where TenDangNhap=@TenDangNhap";
            OleDbCommand cmd = new OleDbCommand(str, cn);

            OleDbParameter para1 = new OleDbParameter("@TenCongTy", tmp.TenCongTy);
            OleDbParameter para2 = new OleDbParameter("@DiaChi", tmp.DiaChi);
            OleDbParameter para3 = new OleDbParameter("@SoDienThoai", tmp.SoDienThoai);
            OleDbParameter para4 = new OleDbParameter("@TenDangNhap", tmp.TenDangNhap);

            cmd.Parameters.Add(para1);
            cmd.Parameters.Add(para2);
            cmd.Parameters.Add(para3);
            cmd.Parameters.Add(para4);

            cmd.ExecuteNonQuery();

            cn.Close();
        }

        public void Delete(CongTyDTO tmp)
        {
            OleDbConnection cn = Sql_DataProvider.SQLConnect();
            string str = "delete from CongTy where TenDangNhap=@TenDangNhap";
            OleDbCommand cmd = new OleDbCommand(str, cn);

            OleDbParameter para1 = new OleDbParameter("@TenDangNhap", tmp.TenDangNhap);

            cmd.Parameters.Add(para1);

            cmd.ExecuteNonQuery();

            cn.Close();
        }

        public static CongTyDTO GetCongTy(string TenDangNhap)
        {
            CongTyDTO KH = new CongTyDTO();
            string sql = string.Format("select * from [CongTy] where TenDangNhap = '{0}'", TenDangNhap);
            OleDbDataReader Read = Sql_DataProvider.SQL_ExcuteQueryReader(sql);
            if (Read.Read())
            {                
                KH.DiaChi = Read["DiaChi"].ToString();
                KH.SoDienThoai = Read["SoDienThoai"].ToString();
                KH.TenDangNhap = Read["TenDangNhap"].ToString();
                KH.TenCongTy = Read["TenCongTy"].ToString();
                return KH;
            }
            else
            {
                return null;
            }
        }


    }
}